package com.nervous.jiaoli.user.user.impl;

import com.nervous.jiaoli.user.user.Report;
import com.nervous.jiaoli.user.user.User;

import com.nervous.jiaoli.user.user.UserDto;
import com.nervous.jiaoli.user.user.UserService;
import com.nervous.jiaoli.user.user.dao.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public List<User> getUserList() {
        return userDao.findUserList();
    }

    @Override
    public List<User> getUserList(UserDto userDto) {
        return userDao.findUserListByCondition(userDto);
    }

    @Override
    public void addUser(UserDto userDto) {
        Calendar cal =Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        String mon = month<10 ? ("0"+month):(""+month);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        String d = day<10 ? ("0"+day):(""+day);
        String prefix= "U" + year + mon + d;
        String newNum = userDao.findUserNewNum(prefix);
        userDto.setU_id(prefix+newNum);
        userDao.addUser(userDto);
    }

    @Override
    public void delUser(UserDto userDto) {
        userDao.delUser(userDto);
    }

    @Override
    public void updateUser(UserDto userDto) {
        userDao.updateUser(userDto);
    }

    @Override
    public Report countUser() {
        return userDao.findCountUser();
    }
}
